package com.ItemsRent.action;

import java.awt.Image;
import java.io.File;
import java.util.HashMap;
 
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import net.sf.json.JSONObject;

import com.ItemsRent.model.Customer;
import com.ItemsRent.model.ItemTable;
import com.ItemsRent.model.ItemType;
import com.ItemsRent.IService.*;

public class ItemTableAction {
	
	private Integer itemId;
	private String itemName;
	private ItemType itemType;
	private String PtypeName;
	private String itemTypeName;
	private Integer itemPrice;
    private Integer itemNumber;
    private Integer itemStatus;
    private String picture;
    private String itemDiscription;
	private Integer customerId;
	private Integer itemTypeId;
	private String itemPicture;
	private String keyword;
	
	private Customer customer;
	private ItemTable itemTable;
	private IitemTable itemTableService ;
	private HashMap<String, Object> result=new HashMap<String, Object>();
	
	private PictureAction pictureAction = new PictureAction();
	private File upload;			  //封装上传文件域属性
	//private Image image;
	
	private String uploadContentType; //封装上传文件类型
	private String uploadFileName;    //封装上传文件名
	private String savePath;          //封装上传文件的保存路径
	
	private String picturePath;
	//private byte[] pictureArray;	  //图片二进制对象
	
	public String getPicturePath() {
		return picturePath;
	}

	public void setPicturePath(String picturePath) {
		this.picturePath = picturePath;
	}

	public Integer getItemId() {
		return itemId;
	}

	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}

	public ItemType getItemType() {
		return itemType;
	}

	public String getPtypeName() {
		return PtypeName;
	}

	public void setPtypeName(String ptypeName) {
		PtypeName = ptypeName;
	}

	public void setItemType(ItemType itemType) {
		this.itemType = itemType;
	}

	public String getItemTypeName() {
		return itemTypeName;
	}

	public void setItemTypeName(String itemTypeName) {
		this.itemTypeName = itemTypeName;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public Integer getItemPrice() {
		return itemPrice;
	}

	public void setItemPrice(Integer itemPrice) {
		this.itemPrice = itemPrice;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public Integer getItemNumber() {
		return itemNumber;
	}

	public void setItemNumber(Integer itemNumber) {
		this.itemNumber = itemNumber;
	}
	
	public Integer getItemStatus() {
		return itemStatus;
	}

	public void setItemStatus(Integer itemStatus) {
		this.itemStatus = itemStatus;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getItemDiscription() {
		return itemDiscription;
	}

	public void setItemDiscription(String itemDiscription) {
		this.itemDiscription = itemDiscription;
	}

	public Integer getCustomerId() {
		return customerId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	public String getItemPicture() {
		return itemPicture;
	}

	public void setItemPicture(String itemPicture) {
		this.itemPicture = itemPicture;
	}

	public Integer getItemTypeId() {
		return itemTypeId;
	}

	public void setItemTypeId(Integer itemTypeId) {
		this.itemTypeId = itemTypeId;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public ItemTable getItemTable() {
		return itemTable;
	}

	public void setItemTable(ItemTable itemTable) {
		this.itemTable = itemTable;
	}

	public IitemTable getItemTableService() {
		return itemTableService;
	}

	public void setItemTableService(IitemTable itemTableService) {
		this.itemTableService = itemTableService;
	}

	public HashMap<String, Object> getResult() {
		return result;
	}

	public void setResult(HashMap<String, Object> result) {
		this.result = result;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public PictureAction getPictureAction() {
		return pictureAction;
	}

	public void setPictureAction(PictureAction pictureAction) {
		this.pictureAction = pictureAction;
	}

	/**
	 * 添加物品 
	 */
	public String addItemTable(){
		customer=(Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		ItemTypeAction itemTypeAction = (ItemTypeAction) ac
				.getBean("itemTypeAction");
		System.out.println("*******");
		this.itemType=itemTypeAction.FindOneItemTypeByName(itemTypeName);////////还没写方法可调用，这句需要
		//this.itemType=new ItemType(this.itemTypeName,this.PtypeName);
		//this.itemType=new ItemType(this.itemTypeId,this.itemTypeName,this.PtypeName);
		this.itemTable=new ItemTable(itemType, customer, itemName, itemPrice, itemNumber,picture, itemDiscription);
		//this.itemTable.setItemId(itemId);
		//this.itemTable.setCustomer(customer);
		//this.itemTable.setItemName(itemName);
		//this.itemTable.setItemNumber(itemNumber);
		//this.itemTable.setItemPrice(itemPrice);
		//this.itemTable.setItemType(itemType);
		//this.itemTable.setItemDiscription(itemDiscription);
		
		this.picturePath = (String) ServletActionContext.getRequest().getSession().getAttribute("picturePath");//取图片路径
		System.out.println("**"+this.itemTable.getItemId());
		if(this.picturePath!=null){
			File file0 = new File(this.picturePath);
			ServletActionContext.getRequest().getSession().setAttribute("picturePath", picturePath);
			/*
			String Oldpath = this.picturePath;
			String path =Oldpath.substring(0,Oldpath.indexOf("\\")) 
					+ this.itemTable.getItemId()
					+ Oldpath.substring(Oldpath.lastIndexOf("."), Oldpath.length());
			*/
			//String path = file0.getParent() + "\\"  + file0.getName(); //+ this.itemTable.getItemId()
			String path ="upload"+ "/" + file0.getName();
			file0.renameTo(new File(path));
			this.itemTable.setPicture(path);
			this.itemTableService.insert(this.itemTable);
			//System.out.println(path);
			//this.UpdateItemInfomation();
		}
		result.put("code", "003");
		result.put("msg", "信息保存成功");
		return "success";
	}
	
	/**
	 * 删除物品(根据物品ID)
	 */
	public void deleteItemTable(){
		this.itemTable=this.itemTableService.findByItemId(this.itemId);
		this.itemTableService.delete(this.itemTable);
	}
	
	
	/**
	 * 根据物品ID查找物品 ,无参数
	 */
	public String FindItemTableById(){
		this.itemTable=this.itemTableService.findByItemId(this.itemId);
		//System.out.println(this.itemTable.toString());
		result.put("item", this.itemTable);
		return "success";
	}
	
	/**
	 * 根据物品ID查找物品 ,有参数
	 */
	public ItemTable FindItemTableById(Integer id){
		return this.itemTableService.findByItemId(id);
	}
	
	/**
	 * 根据物品名称查找物品 ,无参数
	 */
	public String FindItemTableByItemName(){
		//System.out.println("1");
		List<ItemTable> list = this.itemTableService.findByItemName(this.itemName);
		/*
		for(ItemTable item : list){
			System.out.println(item.toString());
		}
		*/
		result.put("items", list);
		return "success";
	}
	
	/**
	 * 根据物品名称查找物品 ,有参数
	 */
	public String FindItemTableByItemName(String itemName){
		//System.out.println("2");
		List<ItemTable> list = this.itemTableService.findByItemName(itemName); 
		result.put("items", list);
		/*
		for(ItemTable item : list){
			System.out.println(item.toString());
		}
		*/
		return "success";
	}
	
	/**
	 * 根据物品种类的ID查找物品 
	 */
	public String FindItemTableByTypeId(){
		List<ItemTable> list = this.itemTableService.findByItemTypeId(this.itemTypeId);
		result.put("items", list);
		return "success";
	}
	
	public String FindItemTableByTypeName(){
		List<ItemTable> list = this.itemTableService.findByItemTypeName(this.itemTypeName);
		/*
		System.out.println("Item:");
		for(ItemTable item:list){
			System.out.println(item.toString());
		}
		*/
		result.put("items", list);
		return "success";
	}
	
	/**
	 * 根据物品所拥有的用户的ID查找物品 
	 */
	public String FindItemTableByCustomerId(){
		customer=(Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		List<ItemTable> list = this.itemTableService.findByCustomerId(customer.getCustomerId());
		/*
		System.out.println("Item:");
		for(ItemTable item:list){
			System.out.println(item.toString());
		}
		*/
		result.put("items", list);
		return "success";
	}
	
	/**
	 * 所有通过审核的上架物品 
	 */
	public String FindItemCanSee(){
		
	    List<ItemTable> list = this.itemTableService.findAllOnLineGood();
	    /*
	    for(ItemTable item:list){
			System.out.println(item.toString());
		}
		*/
		result.put("items", list);
		return "success";
	}
	
	/**
	 * 找出所有的物品
	 **/
	public String showall(){
		List<ItemTable> list = itemTableService.findAll();
		/*
		System.out.println("All ItemTable: ");
		for(ItemTable item:list){
			System.out.println(item.toString());
		}
		*/
		result.put("items", list);
		return "success";
	}
	
	/**
	 * 根据关键字(物品名或者物品种类名或者物品描述)找出所有的上架商品，有参数 
	 **/
	public String FindByKeyword(){
		List<ItemTable> list = itemTableService.findOnlineGoodsByKeyword(keyword);
		for(ItemTable item:list){
			System.out.println(item.toString());
		}
		System.out.println(list.size());
		result.put("items",list);
		return "success";
	}
	
	/**
	 * 更新物品信息(整个物品更新) 
	 */
	public void updateItemTable(){
		customer=(Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		//this.itemTable=this.itemTableService.findByItemId(this.itemId);
		this.itemType=new ItemType(this.itemTypeName,this.PtypeName);
		//this.itemType=new ItemType(this.itemTypeId,this.itemTypeName,this.PtypeName);
		this.itemTable=new ItemTable(itemType, customer, itemName, itemPrice, itemNumber,picture, itemDiscription);
		this.itemTable.setItemId(this.itemId);
		this.itemTableService.update(this.itemTable);
	}
	
	public void updateItemTable(ItemTable it){
		this.itemTableService.update(it);
	}
	
	/**
	 * 更改物品状态
	 **/
	public String SwitchItemStatus(){
		ItemTable item=this.itemTableService.findByItemId(this.itemId);
		item.setItemStatus(this.itemStatus);
		this.itemTableService.update(item);
		return "success";
	}
	
	/**
	 * 更改物品信息
	 * itemId,itemName,itemPrice,itemNumer,itemPicture,itemDiscription,itemTypeName,upload; 
	 **/
	public String UpdateItemInfomation(){
		ItemTable item=this.itemTableService.findByItemId(this.itemId);
		if(this.itemName!=null)   		item.setItemName(this.itemName);
		if(this.itemPrice>=0)  			item.setItemPrice(this.itemPrice);
		if(this.itemNumber>=0) 			item.setItemNumber(this.itemNumber);
		if(this.itemDiscription!=null)  item.setItemDiscription(this.itemDiscription);
		if(this.itemTypeName!=null)		item.setItemType(new ItemType(this.itemTypeName));
		this.itemTableService.update(item);
		return "success";
	}
	
	/*
	public String getPicture(){
		
	}
	*/
}
